angular.module('app.services')
.factory('Util', [ '$log', '$uibModal','Address' ,function( $log, $uibModal,Address ){
	return{
		popup:function( msg ){
			var modalInstance = $uibModal.open({
				backdrop: true,
				size: 'sm',
				template: '<div class="modal-header"><h4 class="modal-title text-center">{{title}}<i class="ti-close pull-right" ng-click="close()" style="cursor:pointer;"></i></h4></div><div class="modal-body" id="modal-body">{{content}}</div>',
				controller: 'PopupController',
				resolve: {
			        items: function () {
			          return {
			          	title: msg.title || '消息提示',
			          	content: msg.content
			          };
			        }
		        }
			})
		},
		//非空校验，支持数组非空校验
		notEmpty: function( input ){
        	if( angular.isArray( input ) ){
        		for( var i=0;i<input.length;i++ ){
        			if( !input[i] ){
        				return false;
        			}
        		}
        		return true;
        	}else{
        		return !! input;
        	}
        },

        //时间戳转换为日期
	     unixToDate:function(timestamp,isTime){
	        var time = new Date( timestamp * 1000),
	            date = '',
	            month = time.getMonth() + 1,
	            day = time.getDate();

	        date += time.getUTCFullYear() + "-";
	        date += (month < 10 ?'0' + month : month) + "-";
	        date += (day < 10 ?'0' + day : day) + " ";
	        if(isTime){
	          date += (time.getHours() < 10 ?'0' + time.getHours() : time.getHours()) + ":";
	          date += (time.getUTCMinutes() < 10 ? '0' + time.getUTCMinutes() : time.getUTCMinutes()) + ":";
	          date += time.getUTCSeconds() < 10 ? '0' + time.getUTCSeconds() : time.getUTCSeconds();
	        }
	        return date;
	     },
 	    /*获取指定日期，y,m,d为距离当前时间，如：今天是2016-9-12，getDateByQuery(-1，0，0) ＝ 2015-9-12*/
	    getDateByQuery: function(y,m,d, format){
            var date = new Date(),
            year = date.getFullYear(),
            month = date.getMonth()+ 1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getUTCMinutes(),
            second = date.getUTCSeconds(),

            y = y || 0,
            m = m || 0,
            d = d || 0;
            var returnVal = '';
            switch(format){
            	default: returnVal = (year+y)+'-'+(month+m)+'-'+(day+d);break;
            } 
            return returnVal;
	    },
	    //日期转换为时间戳
	    dateToUnix:function(date){
	        var dateFullArr = date.split( ' ',2 ),
	            dateArr = ( dateFullArr[0] ? dateFullArr[0] : '' ).split( '-',3 ),
	            timeArr = ( dateFullArr[1] ? dateFullArr[1] : '' ).split( ':',3 );
	        return (new Date(
	            parseInt(dateArr[0], 10) || null,
	            ( parseInt(dateArr[1], 10) || 1 ) - 1,
	            parseInt(dateArr[2], 10) || null,
	            parseInt(timeArr[0], 10) || null,
	            parseInt(timeArr[1], 10) || null,
	            parseInt(timeArr[2], 10) || null
	          ).getTime()) / 1000;
	    },
        //日期格式转时间戳
		dateTypeToUnix:function(date){
			return date.getTime() / 1000;
		},
		//时间戳转日期格式
		timeToStr:function(now,flag){
			now = new Date(now*1000);
			var year = now.getFullYear();
			var month = now.getMonth() + 1;
			var date = now.getDate();
			var hour = now.getHours();
			var min = now.getMinutes();
			var sec = now.getSeconds();
			if(month<10){month = "0"+month;}
			if(date<10){date = "0"+date};
			if(hour<10) {hour = "0"+hour;}
			if(min<10){min = "0"+min;}
			if(sec<10){sec = "0"+sec;}
			if(flag==1) {
				return date+'/'+ month + "/" +year;
			}else if(flag==2) {
				return  hour + ":" + min;
			}else if(flag==3) {
				return  year + "-" + month+"-"+date+" "+hour+":"+min;
			}else if(flag==4) {
				var status="上午";
				if(hour>12){
					hour=hour-12;
					status="下午";
				}
				return  hour+":"+min+status;
			}else if(flag==5){
				var status="上午";
				if(hour>12){
					hour=hour-12;
					status="下午";
				}
				return  status+hour+":"+min;
			}else if(flag==6){
				var status="上午";
				if(hour>12){
					hour=hour-12;
					status="下午";
				}
				return  year + "-" + month+"-"+date+" "+status+hour+":"+min;
			}else if(flag==7){
				return  year + "-" + month+"-"+date+" "+hour+":"+min+":"+sec;
			}else {
				return month + "/" + month+"/"+"";
			}
		},
		isEmptyObject: function(e){
		    var t;  
		    for (t in e)  
		        return !1;  
		    return !0  
		},
		getWindowsParams: function(key){
	        var params = window.location.search;
	        if( !params ) return;
	        var params = params.split('?')[1].split('/')[0];
	        var paramsArray = params.split('&');
	        var tempObj = {};
	        for( var i = 0;i<paramsArray.length;i++){
	          var tempArr = paramsArray[i].split('=');
	          tempObj[tempArr[0]] = tempArr[1];
	        }
	        $log.debug( 'tempObj', tempObj );
	        return tempObj[key] || '';
	    }
	}
}]);
